//1.导入Vue和VueRouter的包
import Vue from 'vue'
import VueRouter from 'vue-router'

import config from '@/config/router.config.js'

// 2.调用Vue.user()函数，把VueRouter安装为Vue的插件
Vue.use(VueRouter)

//3.创建路由实例对象
const router = new VueRouter({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: config,
})
//为router实例对象声明全局前置守卫
router.beforeEach((to, from, next) => {
  if (router.getRoutes().length === 5) {
    console.log('addRoutes....')
    const routes = [
      {
        path: '/',
        component: () => import('@/components/RouteView.vue'),
        redirect: 'home',
        children: [
          {
            path: '/home',
            name: 'home',
            component: () => import('@/components/Home.vue'),
          },
          {
            path: '/mes',
            name: 'mes',
            component: () => import('@/components/RouteView.vue'),

            meta: {
              componentName: 'RouteView',
              internalOrExternal: false,
              keepAlive: false,
              title: '制造执行',
            },
            children: [
              {
                path: '/mes/material',
                name: 'mes-material',
                component: () => import('@/components/RouteView.vue'),
                meta: {
                  componentName: 'RouteView',
                  internalOrExternal: false,
                  keepAlive: false,
                  title: '物料管理',
                },
                children: [
                  {
                    path: '/mes/material/MesMaterialConfigureList',
                    name: 'mes-material-MesMaterialConfigureList',
                    component: () => import('@/components/about/Tab1.vue'),
                    meta: {
                      componentName: 'Tab1',
                      internalOrExternal: false,
                      keepAlive: false,
                      title: '物料入库',
                    },
                  },
                ],
              },
            ],
          },
        ],
      },
    ]
    routes.forEach((item) => router.addRoute(item))
    next({ ...to, replace: true })
  } else {
    //to 将要访问的路由信息对象
    //from 是将要离开的路由信息对象
    //next 是一个函数，调用next()表示放行，允许这次路由导航
    next()
  }
})

// 4.向外共享路由的实例对象
export default router
